﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using NUnit.Framework.Constraints;
using System.Globalization;

namespace TimScripts.BF4Rcon.Test
{
    public class DynamicRconObjectTests
    {
        private dynamic Data;
        private TestObject RealData;

        [SetUp]
        public void SetUp()
        {
            Data = RealData = new TestObject();
        }

        [Test]
        public void TestObjectIsCaseInsensitive()
        {
            string id = "test";
            string value = "result";

            Add(id, value);

            Assert.AreEqual(value, Data.test);
            Assert.AreEqual(value, Data.Test);
            Assert.AreEqual(value, Data.tESt);
        }

        [Test]
        public void GetsString()
        {
            string id = "test";
            string value = "result";

            Add(id, value);

            Assert.AreEqual(value, Data.test);
        }

        [Test]
        public void GetsInteger()
        {
            string id = "test";
            int value = 1337;

            Add(id, value.ToString(CultureInfo.InvariantCulture));

            Assert.AreEqual(value, Data.test);
        }

        [Test]
        public void GetsDouble()
        {
            string id = "test";
            double value = 1.337;

            Add(id, value.ToString(CultureInfo.InvariantCulture));

            Assert.AreEqual(value, Data.test);
        }

        [Test]
        public void GetsBool()
        {
            string id = "test";
            bool value = true;

            Add(id, value.ToString(CultureInfo.InvariantCulture).ToLower());

            Assert.AreEqual(value, Data.test);
        }

        [Test]
        public void NullValueReturnsNull()
        {
            string id = "test";

            Add(id, null);

            Assert.AreEqual(null, Data.test);
        }

        [Test]
        public void AliasWorksCorrectly()
        {
            string alias = "fake";
            string id = "real";
            string value = "val";

            Add(id, value);
            AddAlias(alias, id);

            Assert.AreEqual(value, Data.fake);
        }

        private void Add(string id, string value)
        {
            RealData.Values[id] = value;
        }

        private void AddAlias(string alias, string id)
        {
            RealData.Alias[alias] = id;
        }

        private class TestObject : DynamicRconObject
        {
            public Dictionary<string, string> Values = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

            protected override string GetValue(string id)
            {
                return Values[id];
            }

            public Dictionary<string, string> Alias = new Dictionary<string, string>();

            protected override IDictionary<string, string> Aliases
            {
                get
                {
                    return Alias;
                }
            }
        }
    }
}
